/*
 * Copyright 2010 Yannick Stucki (yannickstucki.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yannickstucki.android.musicqueue.playback;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.yannickstucki.android.musicqueue.SongController;
import com.yannickstucki.android.musicqueue.util.Logger;

/**
 * The service that runs in the background. Due to the user of the singleton
 * pattern for the SongController, this service doesn't actually do anything
 * except making sure our VM instance doesn't get killed while music is playing.
 * 
 * @author Yannick Stucki (yannickstucki@gmail.com)
 * 
 */
public class MusicService extends Service {

  /**
   * Binder between the Activity {@link MusicQueue} and this service.
   */
  private final IBinder binder = new LocalBinder();

  /**
   * Local binder implementation that returns this service.
   */
  public class LocalBinder extends Binder {

    /**
     * Returns this service.
     */
    public MusicService getService() {
      return MusicService.this;
    }
  }

  @Override
  public void onRebind(Intent intent) {
    Logger.out("service on rebind");
    super.onRebind(intent);
  }

  @Override
  public void onStart(Intent intent, int startId) {
    Logger.out("service on start");
    super.onStart(intent, startId);

  }

  @Override
  public boolean onUnbind(Intent intent) {
    Logger.out("service on unbind");
    return super.onUnbind(intent);
  }

  @Override
  public void onCreate() {
    super.onCreate();
    Logger.out("service created");
    this.setForeground(true); // TODO: not supported anymore, different
                              // behaviour in 2.0
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Logger.out("service destroyed");
    SongController.getInstance().onServiceDestroyed();
  }

  // Service stuff
  @Override
  public IBinder onBind(Intent intent) {
    return binder;
  }
}
